package com.yjf.ljr.activity;

import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.adapter.HelpAdapter;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.connection.IjConnect;
import com.yjf.ljr.dataclass.HelpItemDataClass;
import com.yjf.ljr.dataclass.HelpItemDataClass.HelpItemInfo;
import com.yjf.ljr.parser.RequestBuilder;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.SPreferences;

/**
 * 帮助文档
 * 
 * @author dongxie
 */
public class HelpActivity extends IjActivity {

	/** 初始化帮助文档 **/

	@ID("lvHelp")
	public ListView mLvHelp;
	private HelpAdapter mHelpAdapter;
	private List<HelpItemInfo> mData = new ArrayList<HelpItemInfo>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_invest_help);

		initControls();

		showProgressDialog();
		// new IsNeedUpdateTask().execute();
		new HelpInfoTask("").execute();
	}

	/** 初始化控件 **/
	private void initControls() {
		setLeftBtnClick();
		setTitleStr("帮助中心");

		mHelpAdapter = new HelpAdapter(this, mData);
		mLvHelp.setAdapter(mHelpAdapter);
		TextView mTvHelpTitle = (TextView) findViewById(R.id.tvHelpTitle);
		/*
		 * mLvHelp.setOnItemClickListener(new OnItemClickListener() {
		 * 
		 * @Override public void onItemClick(AdapterView<?> arg0, View view, int
		 * postion, long arg3) { view = (RelativeLayout)
		 * findViewById(R.id.rlHelpTitle);
		 * 
		 * for (int i = 0; i < mLvHelp.getChildCount(); i++) { View view2 =
		 * arg0.getChildAt(i); ImageView imageView = (ImageView)
		 * view2.findViewById(R.id.ivHelpArrow); if
		 * ("unfold".equals(imageView.getTag()) && i != postion) { Animation
		 * foldAnim = AnimationUtils.loadAnimation(mContext,
		 * R.anim.help_arrow_fold_rotate); final LinearLayout llHelpDetail =
		 * (LinearLayout) view2.findViewById(R.id.llHelpDetail);
		 * imageView.startAnimation(foldAnim); imageView.setTag("fold");
		 * 
		 * ScaleAnimation animaHide = new ScaleAnimation(1.0f, 1.0f, 1.0f, 0.0f,
		 * Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.0f);
		 * animaHide.setDuration(200);// 设置动画持续时间
		 * 
		 * llHelpDetail.startAnimation(animaHide);
		 * animaHide.setAnimationListener(new AnimationListener() {
		 * 
		 * @Override public void onAnimationStart(Animation animation) { }
		 * 
		 * @Override public void onAnimationRepeat(Animation animation) { }
		 * 
		 * @Override public void onAnimationEnd(Animation animation) {
		 * llHelpDetail.setVisibility(View.GONE); } }); }
		 * 
		 * } } });
		 */
	}

	/** 检查是否需要更新 **/
	class IsNeedUpdateTask extends AsyncTask<Void, Void, String> {
		private HelpItemDataClass dc = new HelpItemDataClass();

		@Override
		protected String doInBackground(Void... params) {
			String lastUpdateTime = (String) SPreferences.getData(HelpActivity.this, "0",
					SPreferences.KEY_HELP_UPDATE_TIME);
			RequestObject reqObj = new RequestObject();
			reqObj.method = "appHelpRefresh";
			reqObj.map.put("updateTime", lastUpdateTime);
			return requestDataAndGetErrorMsg(reqObj, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			if ("1".equals(dc.code)) {
				new HelpInfoTask("").execute();
			} else {// 读取已存储数据
				String data = (String) SPreferences.getData(HelpActivity.this, "", SPreferences.KEY_HELP_CENTER_DATA);
				new HelpInfoTask(data).execute();
			}
		}
	}

	class HelpInfoTask extends AsyncTask<Void, Void, String> {

		private String data = "";
		private HelpItemDataClass dc = new HelpItemDataClass();

		public HelpInfoTask(String data) {
			super();
			this.data = data;
		}

		@Override
		protected String doInBackground(Void... params) {
			RequestObject reqObj = new RequestObject();
			reqObj.method = "HelpContents";

			String failStr = "";
			String xmlStr = "";
			try {
				if (TextUtils.isEmpty(data)) { // 需要请求服务器
					String requestUrl = RequestBuilder.build(reqObj);
					xmlStr = IjConnect.getRequestResult(requestUrl);
					if (!TextUtils.isEmpty(xmlStr)) {
						SPreferences.saveData(HelpActivity.this, xmlStr, SPreferences.KEY_HELP_CENTER_DATA);
					}
				} else {
					xmlStr = data;
				}
				dc.getDataClassFromStr(xmlStr);

				if (dc.code == null) {
					failStr = getResources().getString(R.string.loaddata_exception);
				} else if (!dc.code.equals(CommonData.RESULT_SUCCESS)) {
					failStr = dc.msg;
				}
			} catch (Exception e) {
				failStr = getResources().getString(R.string.loaddata_exception);
				e.printStackTrace();
			}
			return failStr;
		}

		@Override
		protected void onPostExecute(String result) {
			if (!TextUtils.isEmpty(result)) {
				showToast(result);
			} else {
				if (dc.helpList != null && dc.helpList.size() > 0) {
					mData.addAll(dc.helpList);
					mHelpAdapter.notifyDataSetChanged();
				}
				if (!TextUtils.isEmpty(dc.time)) {
					SPreferences.saveData(HelpActivity.this, dc.time, SPreferences.KEY_HELP_UPDATE_TIME);
				}
			}
			dismissProgressDialog();
		}
	}
}
